package com.example.myandroidactualcombat;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myandroidactualcombat.data.Book;
import com.example.myandroidactualcombat.data.ChooseBook;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class ChooseActivity extends AppCompatActivity {
    ListView showChoose;
    List<ChooseBook> chooseBookList;
    List<Book> bookList;
    ArrayList<String> dataList;
    ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose);
        initView();
    }
    private void initView() {
        showChoose = (ListView) findViewById(R.id.showChoose);
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        int userid=prefs.getInt("userid",0);
        chooseBookList= LitePal.where("userid=?",""+userid).find(ChooseBook.class);
        ArrayList<Integer>ids=new ArrayList<>();
        for (int i=0;i<chooseBookList.size();i++){
            ids.add(chooseBookList.get(i).getBookid());
            Log.i("tt", "initViewdadajj"+chooseBookList.get(i).getBookid());
        }
        bookList=LitePal.findAll(Book.class);
        dataList=new ArrayList<>();
        for (int j=0;j<bookList.size();j++){
            Book book = bookList.get(j);
            if (ids.contains(book.getId())){
                dataList.add(book.getBookname());
                Log.i("TAG", "initV"+book.getBookname());
            }
        }
        adapter=new ArrayAdapter<>(ChooseActivity.this,android.R.layout.simple_list_item_1,dataList);
        showChoose.setAdapter(adapter);
    }
}
